package com.cy.dda.service.statistics.impl;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.dao.mapper.cases.CasesMapper;
import com.cy.dda.dao.mapper.customer.CustomerRiskMapper;
import com.cy.dda.dao.mapper.statistics.StatisticsCollectionRecordMapper;
import com.cy.dda.dao.mapper.statistics.StatisticsRepayRecordMapper;
import com.cy.dda.model.dto.customer.StatisticsCustomerRiskCountDTO;
import com.cy.dda.model.enums.customer.CustomerRiskLevel;
import com.cy.dda.model.query.statistics.StatisticsCasesCountQuery;
import com.cy.dda.model.query.statistics.StatisticsCollectionRecordCountQuery;
import com.cy.dda.model.query.statistics.StatisticsCollectionRecordQuery;
import com.cy.dda.model.query.statistics.StatisticsRepayRecordCountQuery;
import com.cy.dda.model.query.statistics.StatisticsRepayRecordQuery;
import com.cy.dda.model.vo.statistics.StatisticsCasesCountVO;
import com.cy.dda.model.vo.statistics.StatisticsCollectionRecordCountVO;
import com.cy.dda.model.vo.statistics.StatisticsCollectionRecordVO;
import com.cy.dda.model.vo.statistics.StatisticsCustomerRiskCountVO;
import com.cy.dda.model.vo.statistics.StatisticsRepayRecordCountVO;
import com.cy.dda.model.vo.statistics.StatisticsRepayRecordVO;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.statistics.DataStatisticsService;

/**
 * 数据统计
 *
 * @author: tanyilin
 * @create: 2018年11月5日 上午10:34:15
 */
@Service
public class DataStatisticsServiceImpl extends BaseService implements DataStatisticsService {
	
	@Autowired
	private StatisticsRepayRecordMapper statisticsRepayRecordMapper;
	@Autowired
	private StatisticsCollectionRecordMapper statisticsCollectionRecordMapper;
	@Autowired
	private CasesMapper casesMapper;
	@Autowired
	private CustomerRiskMapper customerRiskMapper;

	@Override
	public StatisticsRepayRecordCountVO selectStatisticsRepayRecordCountVO(StatisticsRepayRecordCountQuery query) {
		//当前时间
		Date nowDate = new Date();
		//本日开始时间
		Date toDayStartTime = CommonUtils.getDayStartTime(nowDate);
		//本周开始时间
		Date thisWeekStartTime = CommonUtils.getDayStartTime(CommonUtils.getThisWeekMonday(nowDate));
		//本月开始时间
		Date thisMonthStartTime = CommonUtils.getDayStartTime(CommonUtils.getMonthStartTime(nowDate));
		
		query.setToDayStartTime(toDayStartTime);
		query.setThisWeekStartTime(thisWeekStartTime);
		query.setThisMonthStartTime(thisMonthStartTime);
		
		StatisticsRepayRecordCountVO vo = statisticsRepayRecordMapper.selectStatisticsRepayRecordCountVO(query);
		
		return vo;
	}

	@Override
	public List<StatisticsRepayRecordVO> selectStatisticsRepayRecordVO(StatisticsRepayRecordQuery query) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		List<StatisticsRepayRecordVO> newList = new ArrayList<>();
		
		Date endDate = new Date();
		Date startDate = CommonUtils.addTime(endDate, -30, Calendar.DAY_OF_YEAR);
		startDate = CommonUtils.getDayStartTime(startDate);
		
		query.setStartTime(startDate);
		query.setEndTime(endDate);
		List<StatisticsRepayRecordVO> oldList = statisticsRepayRecordMapper.selectStatisticsRepayRecordVO(query);
		
		int n = CommonUtils.diffDays(startDate, endDate);
		int num = CommonUtils.isEmpty(oldList) ? 0 : oldList.size();
		int temp = 0;
		
		for(int i = 0; i < n; i++) {
			if(temp < num) {
				if(startDate.compareTo(oldList.get(temp).getDate()) < 0) {
					//填补空数据
					StatisticsRepayRecordVO vo = new StatisticsRepayRecordVO();
					vo.setCount(0);
					vo.setAmount(BigDecimal.ZERO);
					vo.setDateFormatStr(sdf.format(startDate));
					newList.add(vo);
				}else if(startDate.compareTo(oldList.get(temp).getDate()) == 0) {
					//相等的时候，直接放进newList
					StatisticsRepayRecordVO vo = oldList.get(temp);
					vo.setDateFormatStr(sdf.format(startDate));
					vo.setDate(null);
					newList.add(vo);
					//下标往前推移
					temp++;
				}
				
			}else if(temp >= num && startDate.compareTo(endDate) <= 0) {
				// 填补空数据
				StatisticsRepayRecordVO vo = new StatisticsRepayRecordVO();
				vo.setCount(0);
				vo.setAmount(BigDecimal.ZERO);
				vo.setDateFormatStr(sdf.format(startDate));
				newList.add(vo);
			}
			//日期往下一天推移
			startDate = CommonUtils.addTime(startDate, 1, Calendar.DAY_OF_YEAR);
		}
		
		return newList;
	}

	@Override
	public StatisticsCollectionRecordCountVO selectStatisticsCollectionRecordCountVO(StatisticsCollectionRecordCountQuery query) {
		//当前时间
		Date nowDate = new Date();
		//本日开始时间
		Date toDayStartTime = CommonUtils.getDayStartTime(nowDate);
		//本周开始时间
		Date thisWeekStartTime = CommonUtils.getDayStartTime(CommonUtils.getThisWeekMonday(nowDate));
		//本月开始时间
		Date thisMonthStartTime = CommonUtils.getDayStartTime(CommonUtils.getMonthStartTime(nowDate));
		
		query.setToDayStartTime(toDayStartTime);
		query.setThisWeekStartTime(thisWeekStartTime);
		query.setThisMonthStartTime(thisMonthStartTime);
		
		StatisticsCollectionRecordCountVO vo = statisticsCollectionRecordMapper.selectStatisticsCollectionRecordCountVO(query);
		
		return vo;
	}

	@Override
	public List<StatisticsCollectionRecordVO> selectStatisticsCollectionRecordVO(StatisticsCollectionRecordQuery query) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		List<StatisticsCollectionRecordVO> newList = new ArrayList<>();
		
		Date endDate = new Date();
		Date startDate = CommonUtils.addTime(endDate, -30, Calendar.DAY_OF_YEAR);
		startDate = CommonUtils.getDayStartTime(startDate);
		
		query.setStartTime(startDate);
		query.setEndTime(endDate);
		List<StatisticsCollectionRecordVO> oldList = statisticsCollectionRecordMapper.selectStatisticsCollectionRecordVO(query);
		
		int n = CommonUtils.diffDays(startDate, endDate);
		int num = CommonUtils.isEmpty(oldList) ? 0 : oldList.size();
		int temp = 0;
		
		for(int i = 0; i < n; i++) {
			if(temp < num) {
				if(startDate.compareTo(oldList.get(temp).getDate()) < 0) {
					//填充空数据
					StatisticsCollectionRecordVO vo = new StatisticsCollectionRecordVO();
					vo.setCount(0);
					vo.setDateFormatStr(sdf.format(startDate));
					newList.add(vo);
				}else if(startDate.compareTo(oldList.get(temp).getDate()) == 0) {
					//相等的时候，直接放进newList
					StatisticsCollectionRecordVO vo = oldList.get(temp);
					vo.setDateFormatStr(sdf.format(vo.getDate()));
					vo.setDate(null);
					newList.add(vo);
					//下标往前推移
					temp++;
				}
			}else if(temp >= num && startDate.compareTo(endDate) <= 0) {
				// 填补空数据
				StatisticsCollectionRecordVO vo = new StatisticsCollectionRecordVO();
				vo.setCount(0);
				vo.setDateFormatStr(sdf.format(startDate));
				newList.add(vo);
			}
			//日期往下一天推移
			startDate = CommonUtils.addTime(startDate, 1, Calendar.DAY_OF_YEAR);
		}
		
		return newList;
	}

	@Override
	public StatisticsCasesCountVO selectStatisticsCasesCountVO(StatisticsCasesCountQuery query) {
		StatisticsCasesCountVO vo = casesMapper.selectStatisticsCasesCountVO(query);
		return vo;
	}

	@Override
	public List<StatisticsCustomerRiskCountVO> selectStatisticsCustomerRiskCountVO() {
		List<StatisticsCustomerRiskCountVO> list = customerRiskMapper.selectStatisticsCustomerRiskCountVO();
		
		if(list.stream().noneMatch(c -> CustomerRiskLevel.GENERAL.equals(c.getRiskLevel()))) {
			//如果list里没有GENERAL级别的，新建一个
			StatisticsCustomerRiskCountVO vo = new StatisticsCustomerRiskCountVO();
			vo.setRiskLevel(CustomerRiskLevel.GENERAL);
			vo.setCount(0);
			list.add(vo);
		}
		
		if(list.stream().noneMatch(c -> CustomerRiskLevel.MIDDLE.equals(c.getRiskLevel()))) {
			//如果list里没有MIDDLE级别的，新建一个
			StatisticsCustomerRiskCountVO vo = new StatisticsCustomerRiskCountVO();
			vo.setRiskLevel(CustomerRiskLevel.MIDDLE);
			vo.setCount(0);
			list.add(vo);
		}
		
		if(list.stream().noneMatch(c -> CustomerRiskLevel.HIGH.equals(c.getRiskLevel()))) {
			//如果list里没有HIGH级别的，新建一个
			StatisticsCustomerRiskCountVO vo = new StatisticsCustomerRiskCountVO();
			vo.setRiskLevel(CustomerRiskLevel.HIGH);
			vo.setCount(0);
			list.add(vo);
		}
		 
		list.stream().forEach(c -> {
			c.setItem(c.getRiskLevel().getText());
			c.setRiskLevel(null);
		});
		
		return list;
	}

	@Override
	public List<StatisticsCustomerRiskCountVO> selectStatisticsCustomerRiskCount() {
		
		List<StatisticsCustomerRiskCountVO> list = new ArrayList<>();
		StatisticsCustomerRiskCountDTO dto = customerRiskMapper.selectStatisticsCustomerRiskCountDTO();
		
		//惯性逾期
		StatisticsCustomerRiskCountVO inertiaVo = new StatisticsCustomerRiskCountVO();
		inertiaVo.setItem("惯性逾期");
		inertiaVo.setCount(dto.getInertiaOverdueCount());
		//多家逾期
		StatisticsCustomerRiskCountVO manyVo = new StatisticsCustomerRiskCountVO();
		manyVo.setItem("多家逾期");
		manyVo.setCount(dto.getManyOverdueCount());
		//惯性逾期 + 多家逾期
		StatisticsCustomerRiskCountVO inertiaAndManyVo = new StatisticsCustomerRiskCountVO();
		inertiaAndManyVo.setItem("惯性逾期 + 多家逾期");
		inertiaAndManyVo.setCount(dto.getInertiaAndManyOverdueCount());
		
		list.add(inertiaVo);
		list.add(manyVo);
		list.add(inertiaAndManyVo);
		
		return list;
	}


}
